<?php
class QdketnapsController extends AppController {

	var $name = 'Qdketnaps';
	var $uses = array('Qdketnap','Sinhvien','Ketnapdv','Chidoansv');

	function index() {
		$this->Qdketnap->recursive = 0;
		$this->set('qdketnaps', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid qdketnap', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('qdketnap', $this->Qdketnap->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Qdketnap->create();
			if ($this->Qdketnap->save($this->data)) {
				$this->Session->setFlash(__('The qdketnap has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The qdketnap could not be saved. Please, try again.', true));
			}
		}
		$sinhviens = $this->Qdketnap->Sinhvien->find('list');
		$ketnapdvs = $this->Qdketnap->Ketnapdv->find('list');
		$this->set(compact('sinhviens', 'ketnapdvs'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid qdketnap', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Qdketnap->save($this->data)) {
				$this->Session->setFlash(__('The qdketnap has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The qdketnap could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Qdketnap->read(null, $id);
		}
		$sinhviens = $this->Qdketnap->Sinhvien->find('list');
		$ketnapdvs = $this->Qdketnap->Ketnapdv->find('list');
		$this->set(compact('sinhviens', 'ketnapdvs'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for qdketnap', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Qdketnap->delete($id)) {
			$this->Session->setFlash(__('Qdketnap deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Qdketnap was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}

	function denghi($chidoan_id= null)
	{
		if(!$chidoan_id)
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		
		if(!empty($this->data))
		{
			//luu ketnapdvs
			$record['Ketnapdv']['ngayketnap']=date('Y-m-d');
			$record['Ketnapdv']['chidoansv_id']=$chidoan_id;
			$kn = $this->Ketnapdv->find('list',array('conditions'=>array('chidoansv_id'=>$chidoan_id,
													'ngayketnap'=>$record['Ketnapdv']['ngayketnap'])));
			if(!empty($kn))
			{
				$idketnapdv= key($kn);
			}
			else
			{
				$this->Ketnapdv->create();
				if($this->Ketnapdv->saveAll($record))
				{
					$idketnapdv = $this->Ketnapdv->id;
				}
				else
				{
					$this->Session->setFlash(__("Lỗi khi lưu kết nạp đoàn viên",true),'error');
					$this->redirect(array('denghi',$chidoan_id));
				}
			}
			$this->data['Qdketnap']['ketnapdv_id']=$idketnapdv;
			$this->data['Qdketnap']['duyet']=0;
			foreach ($this->data['Qdketnap']['checkbox'] as $svid => $svvalue)
			{
				if($svvalue==1)
				{
					$this->data['Qdketnap']['sinhvien_id']=$svid;
					$this->Qdketnap->saveAll($this->data);
				}
			}
			$this->Session->setFlash(__("Đã lưu đề nghị kết nạp đoàn viên",true),'success');	
		}
		$chidoans= $this->Chidoansv->find('all',array('recursive'=>-1,'conditions'=>array('Chidoansv.id'=>$chidoan_id)));
		$svcho = $this->Qdketnap->find('list',array(
									'recursive'=>1,
									'fields'=>array('sinhvien_id'),
									'conditions'=>array('Ketnapdv.duyet'=>0,
														'Ketnapdv.chidoansv_id'=>$chidoan_id)
		));
		$dssv = $this->Sinhvien->Tgiachidoan->find('list',array(
															'fields'=>array('sinhvien_id','sinhvien_id'),
															'conditions'=>array('ngaychuyen'=>NULL,'chidoansv_id'=>$chidoan_id,
																				'NOT'=>array('sinhvien_id'=>$svcho))));
		$this->Sinhvien->recursive=-1;
		$sinhviens = $this->Sinhvien->find('all',array(
									'fields'=>array('id','mssv','hoten','ngaysinh'),
									'conditions'=>array('Sinhvien.id'=>$dssv,
														'OR'=>array(array('ngayvaodoan'=>null),array('ngayvaodoan'=>'0000-00-00'))
														)));
		$this->set(compact('sinhviens','chidoan_id','chidoans'));
	} 

	function duyetknap()
	{
		if(!empty($this->data))
		{
			//luu duyet
			$this->Sinhvien->create();
			$this->Ketnapdv->create();
			foreach ($this->data['Qdketnap']['checkbox'] as $svid => $svvalue)
			{
				if($svvalue==1)
				{
					//luu duyet
					$this->Qdketnap->id = $svid;
					$this->Qdketnap->saveField('duyet', 1);
					
					//luy duyet cho de nghi 
					$this->Ketnapdv->id=$this->data['Qdketnap']['ketnapdv_id'][$svid];
					$this->Ketnapdv->saveField('duyet',1);
					
					//luu ngay vao doan cua doan vien
					$this->Sinhvien->id=$this->data['Qdketnap']['sinhvien'][$svid];
					$this->Sinhvien->saveField('ngayvaodoan', date('Y-m-d',strtotime($this->data['Qdketnap']['ngayduyet'])));
					//$this->Sinhvien->saveField('noivaodoan', .'ĐHCT');
				}
			}
			$this->Session->setFlash(__("Đã lưu duyệt kết nạp đoàn viên",true),'success');	
		}
		
		$cd = $this->Ketnapdv->find('list',array('fields'=>array('chidoansv_id'),
												'conditions'=>array('duyet'=>0)));
		
		$chidoans= $this->Chidoansv->find('list',array(
													'fields'=>array('tenchidoan'),
													'conditions'=>array('id'=>$cd)));
		$this->set(compact('chidoans'));
		$this->Qdketnap->recursive=0;
		$this->paginate = array('conditions'=>array('Ketnapdv.duyet'=>0),
								'limit'=>50,
								'order'=>array('Ketnapdv.ngayketnap'=>'DESC'));
		$this->set('qdketnaps',$this->paginate());		
	}

	function updateds()
	{
		$this->layout = 'ajax';
		$chidoan = $this->data['Qdketnap']['chidoan'];
		$this->Qdketnap->recursive=0;
		if(!empty($chidoan))
			$this->paginate = array('conditions'=>array('Ketnapdv.duyet'=>0,'Ketnapdv.chidoansv_id'=>$chidoan),
								'limit'=>50,
								'order'=>array('Ketnapdv.ngayketnap'=>'DESC'));
		else
			$this->paginate = array('conditions'=>array('Ketnapdv.duyet'=>0),
								'limit'=>50,
								'order'=>array('Ketnapdv.ngayketnap'=>'DESC'));
			
			
		$this->set('qdketnaps',$this->paginate());
	}
	
}
